<?php
class C_Rewrite 
{
	
	private $_routes = array();
	private $_currentRoute = null;
	private $_request = null;
	public function __construct($request)
	{
		$this->_request = $request;
	}
	public function router()
	{
	
		 foreach (array_reverse($this->_routes) as $name => $route) 
		 {
            if ($route->match($this->_request)) 
            {
                $this->_setRequestParams($route->getRequestParams());
                $this->_currentRoute = $name;
                break;
            }
        }

        return $request;
	}
	public function getUrl($module,  $action,  $method,  $params = array(), $query = array())
	{
		
		$zend_config = Zend_Registry::get("zend_config");
		
		$routeName = $zend_config->application->C->router->$module;
		
		$route = $this->getRoute($routeName);
		
		return $route->getUrl($module,  $action,  $method,  $params,$query);
	}
 	public function addRoute($name, $route) 
    {
        
        $this->_routes[$name] = $route;
        
        return $this;
    }

    public function addRoutes($routes) 
    {
        foreach ($routes as $name => $route) 
        {
            $this->addRoute($name, $route);
    	}
        
        return $this;
    }
    public function getRoute($name)
    {
        if (!isset($this->_routes[$name])) {
        	echo "没有该路由";
        }
        
        return $this->_routes[$name];
    }
    public function getCurrentRoute()
    {
        if (!isset($this->_currentRoute)) {
        }
        return $this->getRoute($this->_currentRoute);
    }
    public function getCurrentRouteName()
    {
        if (!isset($this->_currentRoute)) {
         
        }
        return $this->_currentRoute;
    }

    protected function _setRequestParams($params)
    {
       $this->_request->setParams($params);
       $this->_request->setModule($params['module'], 'index');
       $this->_request->setAction($params['action'], 'index');
       $this->_request->setMethod($params['method'], 'index');
    }
}

?>